/**
 * QIBAO Service Inc All Rights Reserved @2018
 */

package com.qibao.toolkit.dao;

import org.hibernate.query.NativeQuery;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 * @author hebiao
 * @version $Id:LockTaskDAO.java, v0.1 2018/12/14 18:43 hebiao Exp $$
 */
@Service
public class UtilDAO {

    @PersistenceContext
    private EntityManager entityManager;


    @Modifying
    @Transactional
    public int lockTask(int threadNum, String topic, String locker, String keyWord) {
        String sql = "UPDATE db_queue_message d SET d.status=2,d.locker=? WHERE d.status=1 AND d"
                + ".topic=?  %s ?";
        String querySql =  String.format(sql,keyWord);
        return entityManager.createNativeQuery(querySql).setParameter(1, locker)
                .setParameter(2, topic)
                .setParameter(3, threadNum).executeUpdate();
    }


    public NativeQuery createNativeQuery(String sql){
        return (NativeQuery)entityManager.createNativeQuery(sql);
    }
}
